﻿using Common;
using Common.EF.Controllers;
using Common.Model;
using Common.Models;
using Common.Utility;
using Common.Utility.AJ;
using Common.Utility.AJ.MobileConfiguration;
using FluentValidation;
using Masuit.Tools.Core.Validator;
using Masuit.Tools.Reflection;
using Microsoft.EntityFrameworkCore;
using Stylet;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using static SkiaSharp.HarfBuzz.SKShaper;

namespace AWSV2.ViewModels
{
    public class CarDetailViewModel : AnnotationValidationScreen<Car>
    {
        private string _title;
        public string Title
        {
            get => _title;
            set => SetAndNotify(ref _title, value);
        }

        private AJErrorInfoDialogParams _errorInfo;
        public AJErrorInfoDialogParams ErrorInfo
        {
            get => _errorInfo;
            set => SetAndNotify(ref _errorInfo, value);
        }

        private bool _addMode;
        public bool AddMode
        {
            get => _addMode;
            set => SetAndNotify(ref _addMode, value);
        }

        private bool _loading;
        public bool Loading
        {
            get => _loading;
            set => SetAndNotify(ref _loading, value);
        }

        private Car _form;
        public override Car Form
        {
            get => _form;
            set => SetAndNotify(ref _form, value);
        }

        private bool _keyboardVisible;
        public bool KeyboardVisible
        {
            get => _keyboardVisible;
            set => SetAndNotify(ref _keyboardVisible, value);
        }

        public List<Customer> CustomerOptions { get; set; }

        public ObservableCollection<string> CARNOPREFIX { get; set; }

        public List<string> A2ZSource { get; set; }

        public List<string> Zero2NineSource { get; set; }

        public CarDetailViewModel(Car form = null)
        {
            ErrorInfo = new AJErrorInfoDialogParams();
            CARNOPREFIX = AJUtil.CARNOPREFIX;
            A2ZSource = new List<string>();
            for (int i = 65; i <= 90; i++)
            {
                A2ZSource.Add(((char)i).ToString());
            }
            Zero2NineSource = new List<string>();
            for (int i = 0; i < 10; i++)
            {
                Zero2NineSource.Add(i.ToString());
            }

            AddMode = form == null;
            Title = AddMode
                ? I18NService.Translate(LangKeys.新增, I18NService.Translate(LangKeys.车辆))
                : I18NService.Translate(LangKeys.编辑_0_, form.PlateNo);
            _form = new Car();
            if (form != null)
            {
                AJUtil.CopyPropertyValues(_form, form);
            }
        }

        protected override async void OnViewLoaded()
        {
            using var db = AJDatabaseService.GetDbContext();
            var pages = await db.Customers.AsNoTracking().ToListAsync();

            CustomerOptions = AJAutoMapperService.Instance()
                .Mapper.Map<List<Common.EF.Tables.Customer>, List<Customer>>(pages);
        }

        public void GenerateNow(string field)
        {
            if (nameof(_form.BeginDate).Equals(field))
            {
                _form.BeginDate = DateTime.Now;
            }
            else
            {
                _form.EndDate = DateTime.Now;
            }
        }

        private bool _canCommit;
        public bool CanCommit
        {
            get
            {
                _canCommit = !Loading;
                return _canCommit;
            }
            set => SetAndNotify(ref _canCommit, value);
        }
        public async void Commit()
        {
            if (!ValidateModel())
            {
                ErrorInfo.Show(string.Join("\r\n", ValidationErrorsByProperty.SelectMany(p => p.Value).Select(p => p.ToString())));
                return;
            }
            Loading = true;
            try
            {
                using var carCtrl = new CarController();
                var result = await carCtrl.Save(AJAutoMapperService.Instance()
                    .Mapper.Map<Car, Common.EF.Tables.Car>(_form));

                RequestClose(true);
            }
            catch (Exception e)
            {
                ErrorInfo.Show(e.Message);
            }
            finally
            {
                Loading = false;
            }
        }

        public void ToggleKeyboard()
        {
            KeyboardVisible = !KeyboardVisible;
        }

        public void InputPlateNo(string c)
        {
            Form.PlateNo += c;
        }
    }

}
